<template>
  <div id="app">
    <a-input
      placeholder="请输入任务"
      class="my_ipt"
      :value="inputValue"
      @change="handelInputChange"
    />
    <a-button type="primary" @click="addToDos">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <a-checkbox :checked="item.done" @change="(e) => {isDone(e,item.id)}">{{ item.info }}</a-checkbox>
        <a slot="actions" @click="removeById(item.id)">删除</a>
      </a-list-item>

      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="key==='all' ? 'primary' : 'default'" @click="toggleBtn('all')">全部</a-button>
          <a-button :type="key==='undone' ? 'primary' : 'default'" @click="toggleBtn('undone')">未完成</a-button>
          <a-button :type="key==='done' ? 'primary' : 'default'" @click="toggleBtn('done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务情况 -->
        <a @click="clean">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'App',
  components: {},
  data() {
    return {}
  },
  created() {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['list', 'inputValue', 'key']),
    ...mapGetters(['unDoneLength', 'infoList'])
  },
  methods: {
    handelInputChange(e) {
      console.log(e.target.value)
      this.$store.commit('inputChange', e.target.value)
    },
    addToDos() {
      if (this.inputValue.trim().length <= 0) {
        this.$message.warning('输入任务不能为空')
      }
      this.$store.commit('addItem')
    },
    removeById(id) {
      console.log(id)
      this.$store.commit('removeItem', id)
    },
    isDone(e, id) {
      console.log(e.target.checked)
      this.$store.commit('checkDone', id)
    },
    toggleBtn(param) {
      // 切换按钮，param表示切换的是哪个按钮
      this.$store.commit('changeBtn', param)
    },
    clean() {
      // 清除已完成的任务
      this.$store.commit('cleanDone')
    }
  }
}
</script>

<style>
#app {
  width: 60%;
  margin: 0 auto;
  margin-top: 10px;
  text-align: center;
  position: relative;
}
#app .my_ipt {
  width: 70%;
  margin-right: 10px;
}
.dt_list {
  position: absolute;
  top: 15px;
}
.footer {
  display: flex;
  justify-content: space-between;
}
</style>
